﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    public class HomeDocumentReport : BaseReport
    {

        public MSWordAgent wrdAgent = null;
        //public MSOutlookAgent emailAgent = null;
        public string filecontent = "";
        public string FullName = "";
        string filePath = Path.Combine(PathHelper.ApplicationPath, @"Report\HLT\HLTPlanningReport.xls");
        public HomeDocumentReport()
            : base()
        {
            wrdAgent = new MSWordAgent();
        }
        public HomeDocumentReport(Dictionary<string, object> dicParamater)
            : base(dicParamater)
        {
            wrdAgent = new MSWordAgent();
        }
        private void SendMail(string subject, string body, string filepath)
        {
            /*
            if (emailAgent == null)
                emailAgent = new MSOutlookAgent();
            List<string> attachments = new List<string>();
            attachments.Add(filepath);
            List<string> recipients = new List<string>();
            recipients.Add("sun.light@live.cn");
            recipients.Add("sun.intex@hotmail.com");
            emailAgent.SendEmailWithAttachments(subject, body, recipients, "sun@intex-consulting.cn",
              attachments);
            */
        } 

        public void WriteWordReport(string fileType, DataRow dwKey)
        {
            try
            {
                BaseFunction m_bf = new BaseFunction();
                
                if (!dsSource.Tables.Contains("KDSTAMM"))
                    return;
                if (dwKey == null)
                {
                    foreach (DataRow dwKD in dsSource.Tables["KDSTAMM"].Rows)
                    {
                        dwKey = dwKD;
                    }
                }
                if (dwKey == null)
                    return;
                string filename = "";
                string Standard = @"JHT\EmailA.docx";
                string strWordFile = @"JHT\EmailA.docx";
                string filePath = Path.Combine(PathHelper.ApplicationPath, @"Report\" + strWordFile);
                string filedate = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString();
                filename = @"报价\" + m_bf.Convert8859P1ToGB2312(dwKey["KDKUNDNA"].ToString().Trim()) + filedate + ".docx";
                wrdAgent.CreateDoc(filePath, Standard, filename);
                //filepath=D:\ZZZDBank\tfcIntexReport\Exe\Report\JHT\EmailA.docx
                //standard=JHT\EmailA.docx
                //docname=报价\Q20120907.docx
                //ReplacebyField(string field, string content)
                string wfield = "";
                string wContent = "";
                wfield = "<Date>";
                wContent = filedate;
                wrdAgent.ReplacebyField(wfield, wContent);
                wfield = "<Name>";
                wContent = m_bf.Convert8859P1ToGB2312(dwKey["KDNAME1"].ToString());
                wrdAgent.ReplacebyField(wfield, wContent);
                wfield = "<Subject>";
                wContent = "信用额预警";
                wrdAgent.ReplacebyField(wfield, wContent);
                wfield = "<Content>";
                wContent = "信用额超标20%";
                wrdAgent.ReplacebyField(wfield, wContent);
                wrdAgent.Save();
                filename = wrdAgent.FullName;
                filecontent = wrdAgent.Read();
                wrdAgent.Close();
                System.Diagnostics.Process.Start("WINWORD.EXE", filename);
                FullName = filename;
                SendMail("信用额预警", filecontent, filename);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}
